/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.welcome;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aeky.client.common.AppParams;
import com.aeky.client.common.DefaultCallback;
import com.aeky.client.rpcservice.PrepareDataRPCService;
import com.aeky.client.rpcservice.PrepareDataRPCServiceAsync;
import com.aeky.client.ui.AekyTreeNode;
import com.aeky.model.BaseMapData;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * @author aeky
 */
public class MainMenuTabContent extends Layout {

    private static PrepareDataRPCServiceAsync prepareDataService = AppParams.getService(PrepareDataRPCService.class);

    private Tree mainMenuTree;

    private TreeGrid mainMenuTreeGrid;

    public MainMenuTabContent() {
        this.instanceWidgets();
        // this.addMember(mainMenuTreeGrid);
        getNodeData();
    }

    private void instanceWidgets() {
        mainMenuTreeGrid = new TreeGrid();
        mainMenuTreeGrid.setWidth100();
        mainMenuTreeGrid.setHeight100();
        mainMenuTreeGrid.setNodeIcon("preference.gif");
        mainMenuTreeGrid.setFolderIcon("help.gif");
        mainMenuTreeGrid.setShowHeader(false);
        mainMenuTreeGrid.setShowOpenIcons(false);
        mainMenuTreeGrid.setAnimateFolders(false);
        mainMenuTreeGrid.setShowDropIcons(false);
        mainMenuTreeGrid.setClosedIconSuffix("");
        mainMenuTreeGrid.setFields(new TreeGridField("menuName"));
    }

    private void getNodeData() {
        prepareDataService.prepareBasicData("admin", new DefaultCallback<BaseMapData>() {
            public void onSuccess(BaseMapData data) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("menuNodeList");
                List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
                AekyTreeNode tn = null;
                for (Map<String, Object> map : list) {
                    tn = new AekyTreeNode(map);
                    treeNodeList.add(tn);
                }
                mainMenuTree = new Tree();
                mainMenuTree.setModelType(TreeModelType.PARENT);
                // mainMenuTree.setRootValue(0);
                mainMenuTree.setIdField("menuId");
                mainMenuTree.setNameProperty("menuName");
                mainMenuTree.setParentIdField("menuParentId");
                mainMenuTree.setData(treeNodeList.toArray(new TreeNode[0]));
                mainMenuTreeGrid.setData(mainMenuTree);
            }
        });
    }

    public TreeGrid getMainMenuTreeGrid() {
        return mainMenuTreeGrid;
    }

}
